package com.iyao_app.mvp.dialog;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.adapter.PopImageAdapter;
import com.iyao_app.entry.HomeData;
import com.iyao_app.entry.Manor;
import com.iyao_app.entry.PageInfo;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.entry.SceneData;
import com.iyao_app.entry.SceneUser;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.mvp.laterly.LatelyPersonsActivity;
import com.iyao_app.mvp.search.SearchActivity;
import com.iyao_app.mvp.signlist.SignListActivity;
import com.iyao_app.util.Contants;
import com.iyao_app.util.DbUtil;
import com.iyao_app.util.TimeFormat;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/27.
 */

public class DialogSceneActivity extends Activity {
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_location)
    TextView mTvLocation;
    @BindView(R.id.iv_head_king)
    ImageView mIvHeadKing;
    @BindView(R.id.sdv_head)
    SimpleDraweeView mSdvHead;
    @BindView(R.id.rl_head)
    RelativeLayout mRlHead;
    @BindView(R.id.lin_name)
    LinearLayout mLinName;
    @BindView(R.id.tv_num_qiandao_renshu)
    TextView mTvNumQiandaoRenshu;
    @BindView(R.id.lin_1)
    LinearLayout mLin1;
    @BindView(R.id.rv_qiandao_renshu)
    RecyclerView mRvQiandaoRenshu;
    @BindView(R.id.iv_qiandao_renshu)
    ImageView mIvQiandaoRenshu;
    @BindView(R.id.lin_2)
    LinearLayout mLin2;
    @BindView(R.id.tv_qiandao_cishu)
    TextView mTvQiandaoCishu;
    @BindView(R.id.lin_3)
    LinearLayout mLin3;
    @BindView(R.id.sv_qian_dao)
    SimpleDraweeView mSvQianDao;
    @BindView(R.id.tv_qiandao_name)
    TextView mTvQiandaoName;
    @BindView(R.id.tv_zai_zhe_qian_dao)
    TextView mTvZaiZheQianDao;
    @BindView(R.id.tv_zai_zhe_qian_dao_time)
    TextView mTvZaiZheQianDaoTime;
    @BindView(R.id.iv_qian_dao_ci_shu)
    ImageView mIvQianDaoCiShu;
    @BindView(R.id.lin_4)
    LinearLayout mLin4;
    @BindView(R.id.tv_lai_guo_hao_you)
    TextView mTvLaiGuoHaoYou;
    @BindView(R.id.lin_5)
    LinearLayout mLin5;
    @BindView(R.id.rv_lai_guo_hao_you)
    RecyclerView mRvLaiGuoHaoYou;
    @BindView(R.id.iv_lai_guo_hao_you)
    ImageView mIvLaiGuoHaoYou;
    @BindView(R.id.lin_6)
    LinearLayout mLin6;
    @BindView(R.id.tv_chang_lai_fu_jing_ren)
    TextView mTvChangLaiFuJingRen;
    @BindView(R.id.lin_7)
    LinearLayout mLin7;
    @BindView(R.id.rv_chang_lai_fu_jing_ren)
    RecyclerView mRvChangLaiFuJingRen;
    @BindView(R.id.iv_chang_lai_fu_jing_renu)
    ImageView mIvChangLaiFuJingRenu;
    @BindView(R.id.lin_8)
    LinearLayout mLin8;
    @BindView(R.id.iv_close)
    ImageView mIvClose;
    @BindView(R.id.lin_root)
    LinearLayout mLinRoot;
    @BindView(R.id.tv_linzhu_name)
    TextView mTvLinzhuName;
    @BindView(R.id.tv_linzhu)
    TextView mTvLinzhu;
    @BindView(R.id.lin_2_have)
    LinearLayout mLin2Have;
    @BindView(R.id.tv_2_no)
    TextView mTv2No;
    @BindView(R.id.lin_4_have)
    LinearLayout mLin4Have;
    @BindView(R.id.tv_4_no)
    TextView mTv4No;
    @BindView(R.id.lin_6_have)
    LinearLayout mLin6Have;
    @BindView(R.id.tv_6_no)
    TextView mTv6No;
    @BindView(R.id.lin_8_have)
    LinearLayout mLin8Have;
    @BindView(R.id.tv_8_no)
    TextView mTv8No;

    private String id;
    private LatLng latLng;
    private PopImageAdapter adapterQianDaoRenShu;
    private PopImageAdapter adapterLaiGuoHaoYou;
    private PopImageAdapter adapterLaiFuJingRen;
    private Manor manor;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_changjing);
        ButterKnife.bind(this);
        init();
    }

    //
//    @Override
//    public void onEnterAnimationComplete() {
//
//        super.onEnterAnimationComplete();
//        animation = AnimationUtils.loadAnimation(this, R.anim.mode_in_);
//        mLinRoot.startAnimation(animation);

//        //从弹簧系统创建一个弹簧
//        // Create a system to run the physics loop for a set of springs.
//        SpringSystem springSystem = SpringSystem.create();
//        Spring spring = springSystem.createSpring();
//        //设置弹簧的开始参数
//        spring.setCurrentValue(0.8);
//        //查看源码可知
//        //public static SpringConfig defaultConfig = fromOrigamiTensionAndFriction(40.0D, 7.0D);
//        //弹簧的默认拉力是40，摩擦是7。这里设置为我们seekBar选择的拉力和摩擦参数
//        spring.setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(40, 5));
//        //给弹簧添加监听，动态设置控件的状态
//        spring.addListener(new SimpleSpringListener() {
//            @Override
//            public void onSpringUpdate(Spring spring) {
//                //设置图片的X,Y的缩放
//                //还可以设置setAlpha,setTranslationX...综合形成复杂的动画
//                mLinRoot.setScaleX((float) spring.getCurrentValue());
//                mLinRoot.setScaleY((float) spring.getCurrentValue());
//            }
//        });
//        //设置结束时图片的参数
//        spring.setEndValue(1);
//
//    }

    private void init() {
        manor = (Manor) getIntent().getSerializableExtra(Contants.THIRD_FIELD);
        latLng = new LatLng(manor.latitude, manor.longitude);
        id = manor.sceneId;
        mTvName.setText(manor.name);
        mTvLocation.setText(manor.address);
        sceneInfo();

        StaggeredGridLayoutManager layout = new StaggeredGridLayoutManager(9, StaggeredGridLayoutManager.VERTICAL);
        adapterQianDaoRenShu = new PopImageAdapter(this, null);
        mRvQiandaoRenshu.setAdapter(adapterQianDaoRenShu);
        mRvQiandaoRenshu.setLayoutManager(layout);

        StaggeredGridLayoutManager layout1 = new StaggeredGridLayoutManager(9, StaggeredGridLayoutManager.VERTICAL);
        adapterLaiGuoHaoYou = new PopImageAdapter(this, null);
        mRvLaiGuoHaoYou.setAdapter(adapterLaiGuoHaoYou);
        mRvLaiGuoHaoYou.setLayoutManager(layout1);

        StaggeredGridLayoutManager layout2 = new StaggeredGridLayoutManager(9, StaggeredGridLayoutManager.VERTICAL);
        adapterLaiFuJingRen = new PopImageAdapter(this, null);
        mRvChangLaiFuJingRen.setAdapter(adapterLaiFuJingRen);
        mRvChangLaiFuJingRen.setLayoutManager(layout2);
    }

    @OnClick({R.id.lin_2, R.id.lin_4, R.id.lin_6, R.id.lin_8, R.id.iv_close, R.id.tv_goto})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {

            case R.id.lin_2:
                //签到的人数
                intent = new Intent(DialogSceneActivity.this, LatelyPersonsActivity.class);
                intent.putExtra(Contants.TYPE, 0);
                intent.putExtra(Contants.OTHER_FIELD, manor);
                startActivity(intent);
                break;
            case R.id.lin_4:
                //签到次数
                intent = new Intent(DialogSceneActivity.this, SignListActivity.class);
                intent.putExtra(Contants.TYPE, id);
                startActivity(intent);

                break;
            case R.id.lin_6:
                //来过这里的好友
                intent = new Intent(DialogSceneActivity.this, LatelyPersonsActivity.class);
                intent.putExtra(Contants.TYPE, 1);
                intent.putExtra(Contants.OTHER_FIELD, manor);
                startActivity(intent);
                break;
            case R.id.lin_8:
                //常来附近的人
                intent = new Intent(DialogSceneActivity.this, LatelyPersonsActivity.class);
                intent.putExtra(Contants.TYPE, 2);
                intent.putExtra(Contants.OTHER_FIELD, manor);
                startActivity(intent);
                break;
            case R.id.iv_close:
                finish();
                break;
            case R.id.tv_goto:
                intent = new Intent();
                intent.putExtra(Contants.TYPE, manor);
                setResult(SearchActivity.RESULT_CODE, intent);
                finish();
                break;
        }
    }

    private void sceneInfo() {
//        id = "636c9e63089c48cd9f50710518a7edce";
        NetUtils.getInstance().sceneInfo(id, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model == null) return;
                    SceneData sceneData = model.getModel();
                    if (sceneData != null) {
                        if (!TextUtils.isEmpty(sceneData.name)) {
                            mTvName.setText(sceneData.name);
                        } else {
                            mTvName.setText("未知领域~");
                        }

                        if (!TextUtils.isEmpty(sceneData.address))
                            mTvLocation.setText(sceneData.address);
                        SceneData.ManorInfoBean manorInfo = sceneData.manorInfo;
                        if (manorInfo != null) {
                            try {
                                Uri uri = Uri.parse(manorInfo.avatarUrl);
                                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                                        .setResizeOptions(new ResizeOptions(100, 100))
                                        .build();
                                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                                        .setOldController(mSdvHead.getController())
                                        .setImageRequest(request)
                                        .build();
                                mSdvHead.setController(controller);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            mTvLinzhuName.setText(manorInfo.nickName);
                            mTvLinzhu.setText(R.string.is_ling_zhu);
                            mIvHeadKing.setVisibility(View.VISIBLE);
                        } else {
                            mTvLinzhuName.setText("");
                            mTvLinzhu.setText(R.string.no_lin_zhu);
                            mIvHeadKing.setVisibility(View.GONE);
                        }
                        SceneData.UserSignInInfoBean userSignInInfo = sceneData.userSignInInfo;
                        if (userSignInInfo != null) {
                            try {
                                Uri uri = Uri.parse(userSignInInfo.avatarUrl);
                                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                                        .setResizeOptions(new ResizeOptions(60, 60))
                                        .build();
                                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                                        .setOldController(mSvQianDao.getController())
                                        .setImageRequest(request)
                                        .build();
                                mSvQianDao.setController(controller);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            mTvQiandaoName.setText(userSignInInfo.nickName);
                            if (!TextUtils.isEmpty(userSignInInfo.signInTime)) {
                                try {
                                    mTvZaiZheQianDaoTime.setText(TimeFormat.getTime(Long.parseLong(userSignInInfo.signInTime)));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        if (TextUtils.isEmpty(sceneData.signInCount)) {
                            mTvQiandaoCishu.setText("0");
                            mLin4Have.setVisibility(View.GONE);
                            mTv4No.setVisibility(View.VISIBLE);
                        } else {
                            if ("0".equals(sceneData.signInCount)) {
                                mTvQiandaoCishu.setText("0");
                                mLin4Have.setVisibility(View.GONE);
                                mTv4No.setVisibility(View.VISIBLE);
                            } else {
                                mLin4Have.setVisibility(View.VISIBLE);
                                mTv4No.setVisibility(View.GONE);
                                mTvQiandaoCishu.setText(sceneData.signInCount);
                            }

                        }
                    }

                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, SceneData.class);
        if (DbUtil.getUser() == null) {
            return;
        }

        String userId = DbUtil.getUser().id;
//        userId = "102651";
        /**
         * 来过这里的好友
         */
        NetUtils.getInstance().currentSceneFriend(userId, this.id, 1, 8,0, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model == null) return;
                    HomeData homeData = model.getModel();
                    if (homeData != null) {
                        List<SceneUser> users = homeData.getUsers();
                        if (users != null)
                            adapterLaiGuoHaoYou.append(users);
                        PageInfo pageInfo = homeData.getPageInfo();
                        if (pageInfo != null) {
                            mTvLaiGuoHaoYou.setText(String.valueOf(pageInfo.count));
                            if (pageInfo.count == 0) {
                                mLin6Have.setVisibility(View.GONE);
                                mTv6No.setVisibility(View.VISIBLE);
                            } else {
                                mLin6Have.setVisibility(View.VISIBLE);
                                mTv6No.setVisibility(View.GONE);
                            }
                        } else {
                            mLin6Have.setVisibility(View.GONE);
                            mTv6No.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, HomeData.class);
        /**
         * 获取在当前场景附近签到的用户
         */
        NetUtils.getInstance().currentSceneRoundUser(userId, this.id, 1, 8,0, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model == null) return;
                    HomeData homeData = model.getModel();
                    if (homeData != null) {
                        List<SceneUser> users = homeData.getUsers();
                        if (users != null)
                            adapterLaiFuJingRen.append(users);
                        PageInfo pageInfo = homeData.getPageInfo();
                        if (pageInfo != null) {
                            mTvChangLaiFuJingRen.setText(String.valueOf(pageInfo.count));
                            if (pageInfo.count == 0) {
                                mLin8Have.setVisibility(View.GONE);
                                mTv8No.setVisibility(View.VISIBLE);
                            } else {
                                mLin8Have.setVisibility(View.VISIBLE);
                                mTv8No.setVisibility(View.GONE);
                            }
                        } else {
                            if (users==null||users.size()==0){
                                mLin8Have.setVisibility(View.GONE);
                                mTv8No.setVisibility(View.VISIBLE);
                            }else {
                                mTvChangLaiFuJingRen.setText(String.valueOf(users.size()));
                                mLin8Have.setVisibility(View.VISIBLE);
                                mTv8No.setVisibility(View.GONE);
                            }
                        }
                    }
                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, HomeData.class);
        /**
         * 当前场景签到用户
         */
        NetUtils.getInstance().currentSceneUser(this.id, 1, 8,0, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model == null) return;
                    HomeData homeData = model.getModel();
                    if (homeData != null) {
                        List<SceneUser> users = homeData.getUsers();
                        if (users != null)
                            adapterQianDaoRenShu.append(users);
                        PageInfo pageInfo = homeData.getPageInfo();
                        if (pageInfo != null) {
                            mTvNumQiandaoRenshu.setText(String.valueOf(pageInfo.count));
                            if (pageInfo.count == 0) {
                                mLin2Have.setVisibility(View.GONE);
                                mTv2No.setVisibility(View.VISIBLE);
                            } else {
                                mLin2Have.setVisibility(View.VISIBLE);
                                mTv2No.setVisibility(View.GONE);
                            }
                        } else {
                            mLin2Have.setVisibility(View.GONE);
                            mTv2No.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, HomeData.class);
    }

    @Override
    public void finish() {
        // TODO Auto-generated method stub
        super.finish();
        //关闭窗体动画显示
        this.overridePendingTransition(0, R.anim.dialog_activity_exit);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
